<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="SpEL 表达式的使用SpEl 表达式主要用在bean的定义是上 基于xml的配置 1234&amp;lt;bean id=&quot;numberGuess&quot; class=&quot;org.spring.samples.NumberGuess&quot;&amp;gt;    &amp;lt;property name=&quot;randomNumber&quot; value=&quot;#&amp;#123; T(java.lang.Math).random() * 100.0">
<meta property="og:type" content="article">
<meta property="og:title" content="kyssion-blog">
<meta property="og:url" content="http://yoursite.com/2018/10/09/spring5-SpEL表达式的使用/index.html">
<meta property="og:site_name" content="kyssion-blog">
<meta property="og:description" content="SpEL 表达式的使用SpEl 表达式主要用在bean的定义是上 基于xml的配置 1234&amp;lt;bean id=&quot;numberGuess&quot; class=&quot;org.spring.samples.NumberGuess&quot;&amp;gt;    &amp;lt;property name=&quot;randomNumber&quot; value=&quot;#&amp;#123; T(java.lang.Math).random() * 100.0">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-10-09T07:22:37.337Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="kyssion-blog">
<meta name="twitter:description" content="SpEL 表达式的使用SpEl 表达式主要用在bean的定义是上 基于xml的配置 1234&amp;lt;bean id=&quot;numberGuess&quot; class=&quot;org.spring.samples.NumberGuess&quot;&amp;gt;    &amp;lt;property name=&quot;randomNumber&quot; value=&quot;#&amp;#123; T(java.lang.Math).random() * 100.0">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/10/09/spring5-SpEL表达式的使用/"/>





  <title> | kyssion-blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">kyssion-blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/spring5-SpEL表达式的使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline"></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="SpEL-表达式的使用"><a href="#SpEL-表达式的使用" class="headerlink" title="SpEL 表达式的使用"></a>SpEL 表达式的使用</h3><p>SpEl 表达式主要用在bean的定义是上</p>
<p>基于xml的配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"numberGuess"</span> <span class="attr">class</span>=<span class="string">"org.spring.samples.NumberGuess"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"randomNumber"</span> <span class="attr">value</span>=<span class="string">"#&#123; T(java.lang.Math).random() * 100.0 &#125;"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- other properties --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>基于注解的配置</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">FieldValueTestBean</span></span></span><br><span class="line">    @Value("#&#123; systemProperties['user.region'] &#125;")</span><br><span class="line">    <span class="keyword">private</span> String defaultLocale;</span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123; systemProperties['user.region'] &#125;"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDefaultLocale</span><span class="params">(String defaultLocale)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.defaultLocale = defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getDefaultLocale</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>基于注解的配置在使用自动装配的方法和构造函数时使用@Value注解配套方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SimpleMovieLister</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> MovieFinder movieFinder;</span><br><span class="line">    <span class="keyword">private</span> String defaultLocale;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">configure</span><span class="params">(MovieFinder movieFinder,</span></span></span><br><span class="line"><span class="function"><span class="params">            @Value(<span class="string">"#&#123; systemProperties['user.region'] &#125;"</span>)</span> String defaultLocale) </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.movieFinder = movieFinder;</span><br><span class="line">        <span class="keyword">this</span>.defaultLocale = defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="语言参考"><a href="#语言参考" class="headerlink" title="语言参考"></a>语言参考</h3><p>spring 内置了一个解析器SpelExpressionParser</p>
<h4 id="基本类型表达式"><a href="#基本类型表达式" class="headerlink" title="基本类型表达式"></a>基本类型表达式</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">ExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line"><span class="comment">// evals to "Hello World"</span></span><br><span class="line">String helloWorld = (String) parser.parseExpression(<span class="string">"'Hello World'"</span>).getValue();</span><br><span class="line"><span class="keyword">double</span> avogadrosNumber = (Double) parser.parseExpression(<span class="string">"6.0221415E+23"</span>).getValue();</span><br><span class="line"><span class="comment">// evals to 2147483647</span></span><br><span class="line"><span class="keyword">int</span> maxValue = (Integer) parser.parseExpression(<span class="string">"0x7FFFFFFF"</span>).getValue();</span><br><span class="line"><span class="keyword">boolean</span> trueValue = (Boolean) parser.parseExpression(<span class="string">"true"</span>).getValue();</span><br><span class="line">Object nullValue = parser.parseExpression(<span class="string">"null"</span>).getValue();</span><br></pre></td></tr></table></figure>
<h4 id="获取对象的属性"><a href="#获取对象的属性" class="headerlink" title="获取对象的属性"></a>获取对象的属性</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> year = (Integer) parser.parseExpression(<span class="string">"Birthdate.Year + 1900"</span>).getValue(context);</span><br><span class="line">String city = (String) parser.parseExpression(<span class="string">"placeOfBirth.City"</span>).getValue(context);</span><br><span class="line">parser.parseExpression(<span class="string">"Officers['advisors'][0].PlaceOfBirth.Country"</span>).setValue(</span><br><span class="line">        societyContext, <span class="string">"Croatia"</span>);</span><br></pre></td></tr></table></figure>
<h4 id="获取数组的属性"><a href="#获取数组的属性" class="headerlink" title="获取数组的属性"></a>获取数组的属性</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">String invention = parser.parseExpression(<span class="string">"Members[0].Inventions[6]"</span>).getValue(</span><br><span class="line">        societyContext, String.class);</span><br><span class="line"></span><br><span class="line"><span class="comment">//创造一个内链表</span></span><br><span class="line"></span><br><span class="line">List listOfLists = (List) parser.parseExpression(<span class="string">"&#123;&#123;'a','b'&#125;,&#123;'x','y'&#125;&#125;"</span>).getValue(context);</span><br><span class="line"></span><br><span class="line"><span class="comment">//创建数组</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span>[][] numbers3 = (<span class="keyword">int</span>[][]) parser.parseExpression(<span class="string">"new int[4][5]"</span>).getValue(context);</span><br></pre></td></tr></table></figure>
<h4 id="内联map"><a href="#内联map" class="headerlink" title="内联map"></a>内联map</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// evaluates to a Java map containing the two entries</span></span><br><span class="line">Map inventorInfo = (Map) parser.parseExpression(<span class="string">"&#123;name:'Nikola',dob:'10-July-1856'&#125;"</span>).getValue(context);</span><br><span class="line">Map mapOfMaps = (Map) parser.parseExpression(<span class="string">"&#123;name:&#123;first:'Nikola',last:'Tesla'&#125;,dob:&#123;day:10,month:'July',year:1856&#125;&#125;"</span>).getValue(context);</span><br></pre></td></tr></table></figure>
<h4 id="调用方法"><a href="#调用方法" class="headerlink" title="调用方法"></a>调用方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">boolean</span> isMember = parser.parseExpression(<span class="string">"isMember('Mihajlo Pupin')"</span>).getValue(</span><br><span class="line">        societyContext, Boolean.class);</span><br></pre></td></tr></table></figure>
<h4 id="运算符解析"><a href="#运算符解析" class="headerlink" title="运算符解析"></a>运算符解析</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">boolean</span> trueValue = parser.parseExpression(<span class="string">"'black' &lt; 'block'"</span>).getValue(Boolean.class);</span><br><span class="line"></span><br><span class="line"><span class="keyword">boolean</span> falseValue = parser.parseExpression(</span><br><span class="line">        <span class="string">"'xyz' instanceof T(Integer)"</span>).getValue(Boolean.class);</span><br><span class="line"></span><br><span class="line"><span class="comment">//正则支持</span></span><br><span class="line"><span class="comment">// evaluates to true</span></span><br><span class="line"><span class="keyword">boolean</span> trueValue = parser.parseExpression(</span><br><span class="line">        <span class="string">"'5.00' matches '\^-?\\d+(\\.\\d&#123;2&#125;)?$'"</span>).getValue(Boolean.class);</span><br><span class="line"></span><br><span class="line"><span class="comment">//evaluates to false</span></span><br><span class="line"><span class="keyword">boolean</span> falseValue = parser.parseExpression(</span><br><span class="line">        <span class="string">"'5.0067' matches '\^-?\\d+(\\.\\d&#123;2&#125;)?$'"</span>).getValue(Boolean.class);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意null 在这里视为0 和 空  1&gt; null 返回true -1&gt;null 返回false</p>
</blockquote>
<blockquote>
<p>注意:每个符号运算符也可以被指定为纯粹的字母等效。这避免了所使用的符号对嵌入表达式的文档类型（例如XML文档）具有特殊含义的问题。文本等价物如下所示：lt（&lt;），gt（&gt;），le（⇐），ge（&gt;=），eq（==）， ne（!=），div（/），mod（%），not（!）</p>
</blockquote>
<h4 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// -- AND --</span></span><br><span class="line"><span class="comment">// evaluates to false</span></span><br><span class="line"><span class="keyword">boolean</span> falseValue = parser.parseExpression(<span class="string">"true and false"</span>).getValue(Boolean.class);</span><br><span class="line"><span class="comment">// evaluates to true</span></span><br><span class="line">String expression = <span class="string">"isMember('Nikola Tesla') and isMember('Mihajlo Pupin')"</span>;</span><br><span class="line"><span class="keyword">boolean</span> trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);</span><br><span class="line"><span class="comment">// -- OR --</span></span><br><span class="line"><span class="comment">// evaluates to true</span></span><br><span class="line"><span class="keyword">boolean</span> trueValue = parser.parseExpression(<span class="string">"true or false"</span>).getValue(Boolean.class);</span><br><span class="line"><span class="comment">// evaluates to true</span></span><br><span class="line">String expression = <span class="string">"isMember('Nikola Tesla') or isMember('Albert Einstein')"</span>;</span><br><span class="line"><span class="keyword">boolean</span> trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);</span><br><span class="line"><span class="comment">// -- NOT --</span></span><br><span class="line"><span class="comment">// evaluates to false</span></span><br><span class="line"><span class="keyword">boolean</span> falseValue = parser.parseExpression(<span class="string">"!true"</span>).getValue(Boolean.class);</span><br><span class="line"><span class="comment">// -- AND and NOT --</span></span><br><span class="line">String expression = <span class="string">"isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')"</span>;</span><br><span class="line"><span class="keyword">boolean</span> falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);</span><br></pre></td></tr></table></figure>
<h4 id="数学运算符"><a href="#数学运算符" class="headerlink" title="数学运算符"></a>数学运算符</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Addition</span></span><br><span class="line"><span class="keyword">int</span> two = parser.parseExpression(<span class="string">"1 + 1"</span>).getValue(Integer.class); <span class="comment">// 2</span></span><br><span class="line"></span><br><span class="line">String testString = parser.parseExpression(</span><br><span class="line">        <span class="string">"'test' + ' ' + 'string'"</span>).getValue(String.class); <span class="comment">// 'test string'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Subtraction</span></span><br><span class="line"><span class="keyword">int</span> four = parser.parseExpression(<span class="string">"1 - -3"</span>).getValue(Integer.class); <span class="comment">// 4</span></span><br><span class="line"><span class="keyword">double</span> d = parser.parseExpression(<span class="string">"1000.00 - 1e4"</span>).getValue(Double.class); <span class="comment">// -9000</span></span><br><span class="line"><span class="comment">// Multiplication</span></span><br><span class="line"><span class="keyword">int</span> six = parser.parseExpression(<span class="string">"-2 * -3"</span>).getValue(Integer.class); <span class="comment">// 6</span></span><br><span class="line"><span class="keyword">double</span> twentyFour = parser.parseExpression(<span class="string">"2.0 * 3e0 * 4"</span>).getValue(Double.class); <span class="comment">// 24.0</span></span><br><span class="line"><span class="comment">// Division</span></span><br><span class="line"><span class="keyword">int</span> minusTwo = parser.parseExpression(<span class="string">"6 / -3"</span>).getValue(Integer.class); <span class="comment">// -2</span></span><br><span class="line"><span class="keyword">double</span> one = parser.parseExpression(<span class="string">"8.0 / 4e0 / 2"</span>).getValue(Double.class); <span class="comment">// 1.0</span></span><br><span class="line"><span class="comment">// Modulus</span></span><br><span class="line"><span class="keyword">int</span> three = parser.parseExpression(<span class="string">"7 % 4"</span>).getValue(Integer.class); <span class="comment">// 3</span></span><br><span class="line"><span class="keyword">int</span> one = parser.parseExpression(<span class="string">"8 / 5 % 2"</span>).getValue(Integer.class); <span class="comment">// 1</span></span><br><span class="line"><span class="comment">// Operator precedence</span></span><br><span class="line"><span class="keyword">int</span> minusTwentyOne = parser.parseExpression(<span class="string">"1+2-3*8"</span>).getValue(Integer.class); <span class="comment">// -21</span></span><br></pre></td></tr></table></figure>
<h4 id="属性赋值"><a href="#属性赋值" class="headerlink" title="属性赋值"></a>属性赋值</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String aleks = parser.parseExpression(</span><br><span class="line">        <span class="string">"Name = 'Alexandar Seovic'"</span>).getValue(inventorContext, String.class);</span><br></pre></td></tr></table></figure>
<h4 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Inventor einstein = p.parseExpression(</span><br><span class="line">        <span class="string">"new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')"</span>)</span><br><span class="line">        .getValue(Inventor.class);</span><br></pre></td></tr></table></figure>
<h4 id="三元运算符和猫王运算符"><a href="#三元运算符和猫王运算符" class="headerlink" title="三元运算符和猫王运算符"></a>三元运算符和猫王运算符</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">String falseString = parser.parseExpression(</span><br><span class="line">        <span class="string">"false ? 'trueExp' : 'falseExp'"</span>).getValue(String.class);</span><br><span class="line"></span><br><span class="line">String name = parser.parseExpression(<span class="string">"name?:'Unknown'"</span>).getValue(String.class);</span><br><span class="line"><span class="comment">//上面等价于</span></span><br><span class="line"></span><br><span class="line">String name = parser.parseExpression(<span class="string">"name==null?name:'Unknown'"</span>).getValue(String.class);</span><br></pre></td></tr></table></figure>
<h4 id="集合选择"><a href="#集合选择" class="headerlink" title="集合选择"></a>集合选择</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">List&lt;Inventor&gt; list = (List&lt;Inventor&gt;) parser.parseExpression(</span><br><span class="line">        <span class="string">"Members.?[Nationality == 'Serbian']"</span>).getValue(societyContext);</span><br><span class="line">Map newMap = parser.parseExpression(<span class="string">"map.?[value&lt;27]"</span>).getValue();</span><br></pre></td></tr></table></figure>
<p>着表达式将会选择 List Menbers中 Nationality 为 Serbian的所有元素</p>
<blockquote>
<p>引申:除了返回所有选定的元素之外，还可以检索第一个或最后一个值。要获得与选择相匹配的第一个条目，语法使用^[…​],要获取语法的最后匹配使用$[…​]。</p>
</blockquote>
<h4 id="在字符串中嵌入表达式模版"><a href="#在字符串中嵌入表达式模版" class="headerlink" title="在字符串中嵌入表达式模版"></a>在字符串中嵌入表达式模版</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">String randomPhrase = parser.parseExpression(</span><br><span class="line">        <span class="string">"random number is #&#123;T(java.lang.Math).random()&#125;"</span>,</span><br><span class="line">        <span class="keyword">new</span> TemplateParserContext()).getValue(String.class);</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/09/spring5-AOP/" rel="next" title="">
                <i class="fa fa-chevron-left"></i> 
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/09/spring-boot2.0(1)启动、运行和结束/" rel="prev" title="">
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">kyssion</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">133</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#SpEL-表达式的使用"><span class="nav-number">1.</span> <span class="nav-text">SpEL 表达式的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#语言参考"><span class="nav-number">2.</span> <span class="nav-text">语言参考</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#基本类型表达式"><span class="nav-number">2.1.</span> <span class="nav-text">基本类型表达式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#获取对象的属性"><span class="nav-number">2.2.</span> <span class="nav-text">获取对象的属性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#获取数组的属性"><span class="nav-number">2.3.</span> <span class="nav-text">获取数组的属性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#内联map"><span class="nav-number">2.4.</span> <span class="nav-text">内联map</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#调用方法"><span class="nav-number">2.5.</span> <span class="nav-text">调用方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#运算符解析"><span class="nav-number">2.6.</span> <span class="nav-text">运算符解析</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#逻辑运算符"><span class="nav-number">2.7.</span> <span class="nav-text">逻辑运算符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数学运算符"><span class="nav-number">2.8.</span> <span class="nav-text">数学运算符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#属性赋值"><span class="nav-number">2.9.</span> <span class="nav-text">属性赋值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#构造函数"><span class="nav-number">2.10.</span> <span class="nav-text">构造函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#三元运算符和猫王运算符"><span class="nav-number">2.11.</span> <span class="nav-text">三元运算符和猫王运算符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#集合选择"><span class="nav-number">2.12.</span> <span class="nav-text">集合选择</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#在字符串中嵌入表达式模版"><span class="nav-number">2.13.</span> <span class="nav-text">在字符串中嵌入表达式模版</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kyssion</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
